library(ggplot2)
g = ggplot(data = diamonds)
g + geom_histogram(aes(x=carat))
g + geom_density(aes(x=carat),fill="grey50")
g1 = ggplot(diamonds,aes(x=carat,y=price))
print(g1 + geom_point())
print(g1 + geom_point(aes(color=color)))
#2种分布图
print(g1 + geom_point(aes(color=color)) + facet_wrap(~color))
print(g1 + geom_point(aes(color=color)) + facet_grid(cut~clarity))

print(ggplot(diamonds,aes(x=carat)) + geom_histogram() + facet_wrap(~color))
#箱线图
print(ggplot(diamonds,aes(y=carat,x=1)) + geom_boxplot())

print(ggplot(diamonds,aes(y=carat,x=cut)) + geom_boxplot())

print(ggplot(diamonds,aes(y=carat,x=cut)) + geom_violin())

print(ggplot(diamonds,aes(y=carat,x=cut)) + geom_point() + geom_violin())

print(ggplot(diamonds,aes(y=carat,x=cut)) + geom_violin() + geom_point())

print(ggplot(economics,aes(x=date,y=pop)) + geom_line())

library(lubridate)

economics$year <- year(economics$date)
economics$month <- month(economics$date,label = TRUE)
econ2000 <- economics[which(economics$year >= 2000),]

library(scales)
#画底层图布局
g <- ggplot(econ2000,aes(x=month,y=pop))
#按年每个月份查看欢迎度的因子大小
g <- g + geom_line(aes(color=factor(year),group=year))
# name the legend "Year"
g <- g + scale_color_discrete(name="Year")
#
g <- g + scale_y_continuous(labels = comma)
g <- g + labs(title = "Population Growth",x="Month",y="Population")
print(g)

library(ggthemes)

g2 <- ggplot(diamonds,aes(x=carat,y=price)) + geom_point(aes(color=color))
print(g2 + theme_economist() + scale_color_economist())
print(g2 + theme_excel() + scale_color_excel())
print(g2 + theme_tufte() )
print(g2 + theme_wsj())



